import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import { message } from 'antd'
import { convertContent } from '../constant/lang-util'

export const refreshInstructorPrograms = instructorPrograms => ({
  type: 'REFRESH_PROGRAM_LIST',
  instructorPrograms
})

export const refreshProgram = program => ({
  type: 'REFRESH_PROGRAM',
  program
})
export const refreshWorkSpacePrograms = data => ({
  type: 'REFRESH_WORKSPACE_PROGRAM_LIST',
  data
})

export const getPrograms = (page = 1, pageSize = 20) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/organizations/programs?page=${--page}&pageSize=${pageSize}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshInstructorPrograms(res.body))
      }
    })()
  }
}
export const getWorkspacePrograms = (page = 1, pageSize = 20) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/organizations/programs/workspace?page=${--page}&pageSize=${pageSize}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshWorkSpacePrograms(res.body))
      }
    })()
  }
}
export const getProgramByProgramId = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshProgram(res.body))
      }
    })()
  }
}

export const addPrograms = (newProgram) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/programs`, newProgram)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(getPrograms())
      }
    })()
  }
}
export const updateProgramLink = (data) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/programs/register/link`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getPrograms(data.page, data.pageSize))
      }
    })()
  }
}

export const deleteProgram = (programId, page, pageSize, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/programs/${programId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        callback()
        dispatch(getPrograms(page, pageSize))
      }
    })()
  }
}

export const editProgram = (program, page, pageSize) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/programs/${program.id}`, program)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getPrograms(page, pageSize))
      }
    })()
  }
}

export const addMyWorkspace = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/programs/${programId}/workspace`)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('添加成功'))
        dispatch(getWorkspacePrograms())
      }
    })()
  }
}
export const deleteMyWorkspace = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/programs/${programId}/workspace`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('移除成功'))
        dispatch(getWorkspacePrograms())
      }
    })()
  }
}

export const copyProgram = (programId,callback) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/programs/${programId}/duplication`)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('复制成功'))
        dispatch(getPrograms())
        callback()
      }
    })()
  }
}

export const copyProgramToOrganization = (programId, organizationId, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/programs/${programId}/organizations/${organizationId}/duplication`)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('复制成功'))
        callback()
      }
    })()
  }
}
